package cn.zhonghuanzhiyuan.sprc.basicinfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.web.client.RestTemplate;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.net.InetAddress;
import java.net.UnknownHostException;

//@EnableCircuitBreaker //开启断路器功能
@EnableDiscoveryClient
//@EnableFeignClients(basePackages = {"cn.zhonghuanzhiyuan.eprs.feign"}) //启用Feign
@MapperScan(basePackages = {"cn.zhonghuanzhiyuan.sprc.basicinfo.dao", "cn.org.bachelor.**.dao"})
@SpringBootApplication(scanBasePackages = {"cn.org.bachelor.**.*", "cn.zhonghuanzhiyuan.sprc.**.*"},
//        scanBasePackageClasses = {cn.org.bachelor.acm.dac.DacAutoConfig.class},
        exclude = {ConfigurationPropertiesRebinderAutoConfiguration.class})
//@EnableHystrix
@EnableScheduling //开启定时任务
@EnableTransactionManagement // 开启注解事务管理
public class SprcBasicInfoApplication implements TransactionManagementConfigurer {

    private static Logger logger = LoggerFactory.getLogger(SprcBasicInfoApplication.class);
    @Resource(name = "txManager")
    private PlatformTransactionManager txManager;


    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(SprcBasicInfoApplication.class, args);
        Environment env = application.getEnvironment();
        logger.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\thttp://localhost:{}\n\t" +
                        "External: \thttp://{}:{}\n\t" +
                        "----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                env.getProperty("server.port"),
                InetAddress.getLocalHost().getHostAddress(),
                env.getProperty("server.port"));
    }

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return txManager;
    }

    // 创建事务管理器
    @Bean(name = "txManager")
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
}
